﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;
namespace Web
{
    public partial class EditCustomer : System.Web.UI.Page
    {
        Helper helper = new Helper();
        CustomerEntity customerEntity = new CustomerEntity();
        CustomerController customerController = new CustomerController();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.PreRender += new EventHandler(UpdateFlashMessage);
            if (Request.QueryString["customer"] != null && Request.QueryString["add"] != null)//Add Customer
            {
                PageTitleLabel.Text = "Add Customer";
                CVLabel.Text = "Customer Detail";
            }

            if (Request.QueryString["customer"] != null && Request.QueryString["edit"] != null)//Edit Customer
            {
                PageTitleLabel.Text = "Edit Customer";
                CVLabel.Text = "Customer Detail";
                BindCustomer();
            }
            
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        private void BindCustomer() 
        {
            int customerId = Convert.ToInt32(Request.QueryString["customerId"]);
            customerEntity = customerController.FindCustomerByCustomerId(customerId);
            NameTextBox.Text = customerEntity.Name;
            ShortNameTextBox.Text = customerEntity.ShortName;
            AddressTextBox.Text = customerEntity.Address;
            PhoneTextBox.Text = customerEntity.Phone;
            FaxTextBox.Text = customerEntity.Fax;
            EmailTextBox.Text = customerEntity.Email;
            ContactPersonTextBox.Text = customerEntity.ContactPerson;
            ZipCodeTextBox.Text = customerEntity.ZipCode;
            NumberTextBox.Text = customerEntity.Number.ToString();
            CodeTextBox.Text = customerEntity.Code;
            RemarkTextBox.Text = customerEntity.Remark;
        }

       
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["customer"] != null && Request.QueryString["add"] != null)//Add Customer
            {
                customerEntity = new CustomerEntity();
                
                customerEntity.Name = NameTextBox.Text;
                customerEntity.ShortName = ShortNameTextBox.Text;
                customerEntity.Address = ShortNameTextBox.Text;
                customerEntity.Phone = PhoneTextBox.Text;
                customerEntity.Fax = FaxTextBox.Text;
                customerEntity.Email = EmailTextBox.Text;
                customerEntity.ContactPerson = ContactPersonTextBox.Text;
                customerEntity.ZipCode = ZipCodeTextBox.Text;
                customerEntity.Number = Convert.ToInt32(NumberTextBox.Text);
                customerEntity.Code = CodeTextBox.Text;
                customerEntity.Remark = RemarkTextBox.Text;
                bool result=customerController.AddCustomer(customerEntity);
                if (result == true)
                {
                    Session["FlashMessage"] = "Success: Create Customer Successful!";
                    Session["FlashMessageType"] = "success";
                }
                else
                {
                    Session["FlashMessage"] = "Error: Create Customer unsuccessful!";
                    Session["FlashMessageType"] = "error";
                }
            }

            if (Request.QueryString["customer"] != null && Request.QueryString["edit"] != null)//Edit Customer
            {
                customerEntity = new CustomerEntity();
                customerEntity.Id = Convert.ToInt32(Request.QueryString["customerId"]);
                customerEntity.Name = NameTextBox.Text;
                customerEntity.ShortName = ShortNameTextBox.Text;
                customerEntity.Address = ShortNameTextBox.Text;
                customerEntity.Phone = PhoneTextBox.Text;
                customerEntity.Fax = FaxTextBox.Text;
                customerEntity.Email = EmailTextBox.Text;
                customerEntity.ContactPerson = ContactPersonTextBox.Text;
                customerEntity.ZipCode = ZipCodeTextBox.Text;

                bool result = customerController.UpdateCustomer(customerEntity);
                if (result == true)
                {
                    Session["FlashMessage"] = "Success: Update Customer Successful!";
                    Session["FlashMessageType"] = "success";
                }
                else
                {
                    Session["FlashMessage"] = "Error: Update Customer unsuccessful!";
                    Session["FlashMessageType"] = "error";
                }
            }
           
        }

        

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerList.aspx");
        }
    }
}